package view;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class StartView extends View {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1308736395982224027L;
	public static JButton start, load, newPlayer, addPlayerButton;
	private static JTextField playerName;
	private static JLabel playerLabel, avatarLabel, chooseAvatar, playerAddedLabel;
	private static JLabel[] avatars;
	private static JComboBox avatarComboBox;
	
	private int selectedAvatar;

	private String[] avatarIndex = { "1", "2", "3", "4", "5" };
	
	public StartView() {
		super("bg1.jpg");
		// TODO Auto-generated constructor stub

		start = new JButton("Start game");
		load = new JButton("Load");
		newPlayer = new JButton("New Player");
		playerName = new JTextField(10);
		playerLabel = new JLabel("Player Name:");
		avatarLabel = new JLabel("Avatar:");
		playerAddedLabel = new JLabel("Player added!");
		addPlayerButton = new JButton("Add Player");
		
		avatarComboBox = new JComboBox<String>(avatarIndex);
		
		avatarComboBox.setLocation(130, 100);
		avatarComboBox.setSize(130, 20);
		avatarComboBox.addActionListener(new avatarListener());
		avatarComboBox.setVisible(false);

		avatars = new JLabel[5];
		
		start.setLocation(20, 20);
		start.setSize(100, 20);
		start.setEnabled(true);
		
		newPlayer.setLocation(130, 20);
		newPlayer.setSize(100, 20);
		
		playerName.setLocation(130, 70);
		playerName.setSize(130, 20);
		playerName.setVisible(false);
		
		playerLabel.setSize(130, 70);
		playerLabel.setLocation(40, 45);
		playerLabel.setVisible(false);
		
		avatarLabel.setSize(130, 70);
		avatarLabel.setLocation(77, 75);
		avatarLabel.setVisible(false);
		
		playerAddedLabel.setSize(130, 70);
		playerAddedLabel.setLocation(130, 20);
		playerAddedLabel.setVisible(false);
		
		addPlayerButton.setSize(100, 20);
		addPlayerButton.setLocation(280, 100);
		addPlayerButton.setVisible(false);
		
		for(int i = 0; i < 5; i++) {

			
			avatars[i] = new JLabel();
			avatars[i].setLocation(130, 100);
			
			avatars[i].setSize(115, 125);
			avatars[i].setIcon(new ImageIcon("img/avatars/" + (i+1) + ".png"));
			
		} 
		
		
		// load button?
		add(start);
		add(load);
		
		add(playerName);
		add(avatarComboBox);
		
		add(newPlayer);
		add(playerLabel);
		add(avatarLabel);
		add(playerAddedLabel);
		
		add(addPlayerButton);
		
		newPlayer.addActionListener(new newPlayerListener());
		
		//start.addActionListener(new startgameListener());
		

	}
	
	public static String getPlayerName() {
		
		return playerName.getText();
		
	}
	
	public static int getSelectedAvatar() {
		
		return Integer.parseInt((String) avatarComboBox.getSelectedItem());
		
	}
	
	class newPlayerListener implements ActionListener {
		
		@Override
		public void actionPerformed(ActionEvent arg0) {
			
			playerName.setVisible(true);
			playerLabel.setVisible(true);
			avatarComboBox.setVisible(true);
			addPlayerButton.setVisible(true);
			avatarLabel.setVisible(true);
			
			add(avatars[0]); //Show avatar 1
			
		}
		
	}
	
	public void playerAdded() {
		
		playerName.setText("");
		playerName.setVisible(false);
		playerLabel.setVisible(false);
		avatarComboBox.setVisible(false);
		avatarComboBox.setSelectedIndex(0);
		addPlayerButton.setVisible(false);
		avatarLabel.setVisible(false);
		remove(avatars[selectedAvatar]);
		
		start.setEnabled(true);
		playerAddedLabel.setVisible(true);
		
	}
	
	class avatarListener implements ActionListener {

		@Override
		public void actionPerformed(ActionEvent arg0) {
			// TODO Auto-generated method stub
			
			remove(avatars[selectedAvatar]);
			
			selectedAvatar = Integer.parseInt((String) avatarComboBox.getSelectedItem()) - 1;
			
			add(avatars[selectedAvatar]);
			
		}
		
		
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		
	}
}
